﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using WebUI.SmartDatingRef;
using SvcUtils;

namespace WebUI {
    public partial class SetLocation : BasePage {
        protected global::System.Web.UI.WebControls.LinkButton cmdSaveChanges;

        protected void cmdSaveChanges_Click(object sender, EventArgs e) {
            CultureInfo enUSCulture = new CultureInfo("en-US");

            double latitude, longitude;
            try {
                latitude = Double.Parse(txtLatitude.Value, enUSCulture);
                longitude = Double.Parse(txtLongitude.Value, enUSCulture);
            } catch (Exception) {
                Utils.showError(errSpan, "Введены некорректные координаты.");
                return;
            }

            OperationStatus setLocRes = Client.SetLocation(SessionKeyHash,
                latitude, longitude);
            if (!Status.SUCCESS(setLocRes)) {
                Utils.showError(errSpan, Status.m(setLocRes));
                return;
            }

            Response.Redirect(Request["ReturnUrl"] == null ? "Dashboard.aspx" :
                Request["ReturnUrl"]);
        }

        protected void Page_Load(object sender, EventArgs e) {
            string name = "";
            int gender = 2;

            ResultWithStatusOfstring gpdRes = Client.GetPersonalData(SessionKeyHash);
            if (Status.SUCCESS(gpdRes.Status)) {
                PersonalData pd = CryptoImpl.readObjectEncrypted<PersonalData>(
                    b(gpdRes.Result), b(SessionKey));
                name = pd.first_name + " " + pd.last_name;
                gender = pd.gender;
            } else if (gpdRes.Status == OperationStatus.NoPersonalData) {
                // no personal data to extract
            } else {
                Utils.showError(errSpan, Status.m(gpdRes.Status));
                pnlMap.Visible = false;
                return;
            }
            
            // default latitude and longitude center the viewer around LETI
            const double DEFAULT_LAT = 59.97567;
            const double DEFAULT_LNG = 30.32171;
            double lat = DEFAULT_LAT, lng = DEFAULT_LNG;

            ResultWithStatusOfNullableOfPairOfdoubledoubleswc4fAsa5F2dSckg getLocRes =
                Client.GetLocation(SessionKeyHash);
            
            if (getLocRes.Status == OperationStatus.NoLocation) {
                // no data given for location yet.
            } else if (Status.SUCCESS(getLocRes.Status)) {
                // data is given, read it
                lat = getLocRes.Result.Value.First;
                lng = getLocRes.Result.Value.Second;
            } else {
                // fatal location data error
                Utils.showError(errSpan, Status.m(getLocRes.Status));
                pnlMap.Visible = true;
                return;
            }

            Utils.hideError(errSpan);

            ScriptManager.RegisterStartupScript(this, typeof(SetLocation), "location_init",
                "initializeMap({mapId:'map'," +
                    "txtLatId:'txtLatitude'," +
                    "txtLngId:'txtLongitude'," +
                    "txtNameId:'txtName',"+
                    "title:'" + name + "'," +
                    "gender:" + gender + "," +
                    "latitude:" + lat.ToString(new CultureInfo("en-US")) + "," +
                    "longitude:" + lng.ToString(new CultureInfo("en-US")) + "," +
                    "canMoveMyself:true});",
                true);
        }
    }
}